package com.hernie.user.controller;

import com.hernie.user.util.GetAndPost;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/weather")
public class WeatherController {
    @GetMapping("/selectCity")
    public Map<String, Object> selectCity(@RequestParam(required = false, defaultValue = "成都") String cityName) {
//        String url = "http://apis.juhe.cn/simpleWeather/query" +
//                "?city=" + cityName + "&key=d5f452d52936d728bae5b0eaaf41493e";
//        System.out.println(url);
        String s = GetAndPost.requestGet("http://apis.juhe.cn/simpleWeather/query?city=%E8%9A%8C%E5%9F%A0&key=d53da08026aad202d9ea0ec630e1a156&key=d53da08026aad202d9ea0ec630e1a156");
        JSONObject jsonObject = new JSONObject(s);
//        System.out.println(jsonObject);
        Map<String, Object> map = new HashMap<>();
        map.put("count", 1);
        map.put("msg", "查询成功");
        map.put("code", 0);
        JSONObject jsonObject1 = jsonObject.getJSONObject("result").getJSONObject("realtime");
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonObject1);
        map.put("data", jsonArray.toList());
//        System.out.println(map);
        return map;
    }
}
